﻿using NServiceKit.Redis;
using System;
using System.Collections.Generic;
using System.Linq;

namespace HobSoft.SoccerBet2014.Domain.Implementation.RedisRepository
{
    using IRepositories;
    using Models;
    public class GameMappingRepository : ReadWriteRedisRepository<GameMapping>, IGameMappingRepository
    {
        public GameMappingRepository(PooledRedisClientManager pooledRedisClient)
            : base(pooledRedisClient)
        {

        }
        public IList<GameMapping> GetMany(DateTime afterStartTime, DateTime beforeStartTime)
        {
            using (var redisClient = CreateReadRedisClient())
            {
                var redisBfEvent = redisClient.As<BFEvent>();
                var redisMapping = redisClient.As<GameMapping>();
                var events = redisBfEvent.GetAll().Where(be => be.StartTime > afterStartTime && be.StartTime < beforeStartTime);
                var evtIds = events.Select(ev => ev.EventID);
                return redisMapping.GetAll().AsQueryable().Where(gm => evtIds.Contains(gm.EventID)).ToList();
            }
        }
    }
}
